<template>
    <div class="app-container">
      <wise-crud
        ref="wise-crud"
        :urls="urls"
        :fields="fields"
        :handle="handle"
        :filters="filters"
      >
        <template v-slot:handle="slotProps" label-width="500px">
          <el-button
            v-permission="['superSysOpsAdmin', 'entAdmin']"
            type="primary"
            size="small"
            round
            @click.native.prevent="editHandle(slotProps.row)"
            >编辑
          </el-button>
          <el-button
            type="info"
            size="small"
            round
            @click.native.prevent="viewHandle(slotProps.row)"
            >查看
          </el-button>
          <el-button
            v-permission="['superSysOpsAdmin', 'entAdmin']"
            type="danger"
            size="small"
            round
            @click.native.prevent="removeHandle(slotProps.row)"
            >删除
          </el-button>
          <el-button
            v-permission="['superSysOpsAdmin', 'entAdmin']"
            type="warning"
            size="small"
            round
            @click.native.prevent="setTheme(slotProps.row)"
            >设置主题
          </el-button>
           
          <el-button
          
            type="info"
            size="small"
            round
            @click.native.prevent="screenMonitor(slotProps.row)"
            >项目管理
          </el-button>
        </template>
        <template slot="outHandle">
          <el-col :span="3">
            <!-- <el-button
              v-permission="['superSysOpsAdmin']"
              type="primary"
              @click="createProjectHandle()"
            >
              <i class="el-icon-plus" />
              创建项目
            </el-button> -->
          </el-col>
          <el-col :span="3" style="margin-left: 50px">
            <el-button
              v-permission="[
                'superSysOpsAdmin',
                'entAdmin',
                'proAdmin',
                'formalEmployee',
              ]"
              type="primary"
              @click="createProjectHandleks()"
              style="width: 200px"
            >
              <i class="el-icon-plus" />
              快速创建项目
            </el-button>
          </el-col>
        </template>
      </wise-crud>
  
      <!-- 创建项目 -->
      <CreateProjectForm ref="createProjectForm" />
  
      <!-- 创建项目 -->
      <CreateProjectFormks ref="createProjectFormks" />
      <!-- 主题弹层 -->
      <el-dialog title="设置主题" :visible.sync="dialogVtheme" width="30%">
        <el-form ref="form" :model="themeObj" label-width="80px">
          <el-form-item label="设置主题">
            <el-select v-model="themeObj.code" clearable placeholder="请选择主题">
              <el-option label="绿松石蓝(默认)" value="default" />
              <el-option label="粉绿主题" value="theme2" />
              <el-option label="天蓝主题" value="theme3" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="setThemeHandle">确定</el-button>
            <el-button @click="dialogVtheme = false">取 消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
  
      
    </div>
  </template>
  
  <script>
  import WiseCrud from "@/components/Crud/wiseCrud";
  import { postHandle } from "@/api";
  import WiseFile from "@/components/Crud/components/wiseFile";
  import WiseRegion from "@/components/Crud/components/wiseRegion";
  import WiseDate from "@/components/Crud/components/wiseDate";
  import FileType from "@/components/Crud/components/fileType";
  import CreateProjectForm from "./CreateProjectForm1.vue";
  import CreateProjectFormks from "./createProjectFormks.vue";
  import { setSpaceId } from "../../../../utils/auth";
  
  export default {
    name: "ProviderManage",
    components: {
      FileType,
      WiseCrud,
      WiseRegion,
      WiseDate,
      WiseFile,
      CreateProjectForm,
      CreateProjectFormks
    },
    data() {
      return {
        dqsj: new Date().getTime(),
        form: {
          region: "",
        },
        subView: false,
        craftsId: null,
        dialogVtheme: false,
        urls: {
          list: "/api/v1/welder-sbtx-second/project-info-comb/list",
          add: "/api/v1/welder-sbtx-second/project-info-comb/create",
          show: "/api/v1/welder-sbtx-second/project-info-comb/show/",
          update: "/api/v1/welder-sbtx-second/project-info-comb/update/",
        },
        handle: ["filters"],
        fields: [
          {
            label: "序号",
            type: "index",
            use: ["table"],
          },
          {
            label: "作业单位",
            prop: "name",
            type: "text",
            maxlength: 20,
            use: ["table"],
          },
          {
            label: "项目编号",
            prop: "welderNumber",
            type: "text",
            maxlength: 20,
            use: [ ],
          },
          {
            label: "项目地址",
            prop: "area",
            type: "area",
            objKeys: [
              {
                field: "province",
                key: "province",
                value: "key",
              },
              {
                field: "provinceName",
                key: "province",
                value: "value",
                tableShow: true,
              },
              {
                field: "city",
                key: "city",
                value: "key",
              },
              {
                field: "cityName",
                key: "city",
                value: "value",
                tableShow: true,
              },
              {
                field: "county",
                key: "area",
                value: "key",
              },
              {
                field: "countryName",
                key: "area",
                value: "value",
                tableShow: true,
              },
              {
                field: "street",
                key: "town",
                value: "key",
              },
              {
                field: "streetName",
                key: "town",
                value: "value",
                tableShow: true,
              },
            ],
            use: ["table"],
          },
          {
            label: "详细地址",
            prop: "address",
            type: "text",
            maxlength: 200,
            use: ["table"],
          },
          {
            label: "项目开始时间",
            prop: "startTime",
            type: "date",
            format: "YYYY-MM-DD HH:mm:ss",
            use: ["table"],
          },
          {
            label: "项目结束时间",
            prop: "endTime",
            type: "date",
            format: "YYYY-MM-DD HH:mm:ss",
            use: ["table"],
          },
          {
            label: "动火等级",
            prop: "hotLevel",
            type: "text",
            maxlength: 20,
            use: ["table"],
          },
          {
            label: "操作",
            type: "handle",
            use: ["table"],
            handle: [],
            tableWidth: 340,
          },
        ],
        drawer: false,
        drawer1: false,
        drawer2: false,
        drawer3: false,
        removeLoding: null,
        title: "创建项目",
        typeList: [],
        craftsInfoList: [],
        userList: [],
        object: {
          name: null,
          screenName: null,
          logo: null,
          serialNum: null,
          province: null,
          city: null,
          county: null,
          street: null,
          provinceName: null,
          cityName: null,
          countryName: null,
          streetName: null,
          address: null,
          companyName: null,
          manager: null,
          typeCode: null,
          phone: null,
          file: [],
          remark: null,
          craftsId: null,
          welderNumber: null,
          userId: null,
          startTime: "",
          endTime: "",
          hotLevel: null,
          hotEntName: null,
          repairMethod: null,
          weldAllNumber: null,
          weldFinshNumber: null,
          portraitWeldAllNumber: null,
          portraitWeldFinshNumber: null,
          cinctureWeldAllNumber: null,
          cinctureWeldFinshNumber: null,
          construction: null,
        },
        rules: {
          name: [
            {
              required: true,
              message: "请输入项目名称",
              trigger: "blur",
            },
          ],
          welderNumber: [
            {
              required: true,
              message: "请输入项目编号",
              trigger: "blur",
            },
          ],
          area: [
            {
              required: true,
              message: "请选择地址",
              trigger: "blur",
            },
          ],
          address: [
            {
              required: true,
              message: "请输入详细地址",
              trigger: "blur",
            },
          ],
          startTime: [
            {
              required: true,
              message: "请选择项目开始时间",
              trigger: "blur",
            },
          ],
          endTime: [
            {
              required: true,
              message: "请选择项目结束时间",
              trigger: "blur",
            },
          ],
          hotLevel: [
            {
              required: true,
              message: "请输入动火等级",
              trigger: "blur",
            },
          ],
          hotEntName: [
            {
              required: true,
              message: "请输入动火作业单位",
              trigger: "blur",
            },
          ],
          repairMethod: [
            {
              required: true,
              message: "请输入生产计划",
              trigger: "blur",
            },
          ],
          screenName: [
            {
              required: true,
              message: "请输入大屏名称",
              trigger: "blur",
            },
          ],
        },
        fileList: [],
        themeObj: {
          id: null,
          code: "default",
        },
      };
    },
    computed: {
      spaceId() {
        return this.$store.getters.entSpaceId;
      },
      filters() {
        if (this.$store.state.user.project) {
          return [
            {
              field: "id",
              operator: "EQUALS",
              value: this.$store.state.user.project.id,
            },
          ];
        } else {
          return [];
        }
      },
    },
    mounted() {
      window.location.reload(true);
      this.$nextTick(() => {
        this.$refs["createProjectForm"].$on("refreshTable", () => {
          this.$refs["wise-crud"].handleSearch(0);
        });
      });
    },
    methods: {
      handleAdd() {
        this.title = "创建项目";
        this.fileList = [];
        this.object = {
          name: null,
          screenName: null,
          logo: null,
          serialNum: null,
          area: null,
          provinceCode: null,
          cityCode: null,
          countyCode: null,
          streetCode: null,
          provinceName: null,
          cityName: null,
          countyName: null,
          streetName: null,
          address: null,
          companyName: null,
          manager: null,
          typeCode: null,
          phone: null,
          file: [],
          remark: null,
          craftsId: null,
          welderNumber: null,
          userId: null,
          startTime: "",
          endTime: "",
          hotLevel: null,
          hotEntName: null,
          repairMethod: null,
          weldAllNumber: null,
          weldFinshNumber: null,
          portraitWeldAllNumber: null,
          portraitWeldFinshNumber: null,
          cinctureWeldAllNumber: null,
          cinctureWeldFinshNumber: null,
          construction: null,
        };
        this.drawer = true;
        this.getCraftsInfoList();
      },
      viewScreen() {
        this.$router.push({
          name: "screen",
        });
        // if (this.enterprise.bigScreenType == 1) {
        //   window.open(
        //     "/enterprise/screen?id=" +
        //       this.enterprise.spaceId +
        //       "&img=" +
        //       this.enterprise.bigScreenStr
        //   );
        // } else {
        //   window.open(this.enterprise.bigScreenStr);
        // }
      },
      editHandle(row) {
        this.$refs["createProjectFormks"].editHandle(row);
        this.$refs["createProjectFormks"].drawer = true;
      },
      viewHandle(row) {
        this.getObjectById(row.id);
        this.drawer1 = true;
      },
      removeHandle(row) {
        this.$confirm("您确定要删除该项目, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let removeLoding = this.$loading({
              lock: true,
              text: "删除中,请勿操作!或者刷新页面!",
              spinner: "el-icon-loading",
              background: "rgba(0, 0, 0, 0.7)",
            });
            postHandle(
              "/api/v1/welder-sbtx-second/project-info-comb/remove-project/" +
                row.id,
              {}
            )
              .then((response) => {
                this.$message("删除成功");
                this.dialogVtheme = false;
                this.$refs["wise-crud"].handleSearch(1);
                removeLoding.close();
              })
              .catch((error) => {
                this.loading = false;
                const data = error.response.data;
                for (const key in data) {
                  this.$message.error(this.$t(data[key]));
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      getObjectById(id) {
        this.fileList = [];
        postHandle(this.urls.show + id, {}).then((response) => {
          this.object = response.result;
          this.object.area = {
            province: {
              key: this.object.province,
              value: this.object.provinceName,
            },
            city: {
              key: this.object.city,
              value: this.object.cityName,
            },
            area: {
              key: this.object.county,
              value: this.object.countyName,
            },
            town: {
              key: this.object.street,
              value: this.object.streetName,
            },
          };
        });
      },
      removeById(id) {},
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url;
            if (this.object.id) {
              url = this.urls.update + this.object.id;
            } else {
              url = this.urls.add;
            }
            postHandle(url, {
              obj: this.object,
            })
              .then((response) => {
                this.$message("操作成功");
                this.drawer = false;
                if (this.object.id) {
                  this.$refs["wise-crud"].handleSearch(0);
                } else {
                  this.$refs["wise-crud"].handleSearch(1);
                }
                // this.$refs['wise-crud'].handleSearch()
                // this.$refs['drawer'].closeDrawer()
              })
              .catch((error) => {
                const data = error.response.data;
                for (const key in data) {
                  this.$message.error(this.$t(key + "." + data[key]));
                }
              });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      setValue(objVal) {
        if (objVal && objVal.length > 0) {
          objVal.forEach((item) => {
            this.$set(this.object, item.field, item.value);
          });
        }
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      getCraftsInfoList() {
        const req = {
          d: {
            page: {
              p: 1,
              ps: 9999,
            },
          },
        };
        postHandle("/api/v1/welder-sbtx-second/crafts-info-comb/list", req).then(
          (response) => {
            const data = response.result;
            this.craftsInfoList = data;
          }
        );
      },
  
      screenMonitor(row) {
      
          setSpaceId(row.spaceId);
          window.location.href = "/project";
        
      },
      setTheme(row) {
        this.themeObj.id = row.id;
        this.themeObj.code = row.code ? row.code : "default";
        this.dialogVtheme = true;
      },
      setThemeHandle() {
        postHandle(
          `/api/v1/welder-sbtx-second/project-info-comb/update-project-code/${this.themeObj.id}`,
          {
            d: {},
            obj: this.themeObj,
          }
        )
          .then((response) => {
            this.$message("操作成功");
            this.dialogVtheme = false;
            this.$refs["wise-crud"].handleSearch(1);
          })
          .catch((error) => {
            const data = error.response.data;
            for (const key in data) {
              this.$message.error(this.$t(key + "." + data[key]));
            }
          });
      },
      // createProjectHandle() {
      //   this.$refs["createProjectForm"].title = "创建项目";
      //   this.$refs["createProjectForm"].drawer = true;
      // },
      createProjectHandleks() {
        this.$refs["createProjectFormks"].title = "快速创建项目";
        this.$refs["createProjectFormks"].drawer = true;
      },
    },
  };
  </script>
  
  <style scoped>
  .select-add .el-form-item {
    margin-bottom: 18px;
  }
  </style>
  